package setting

import (
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/setting"
	"likeadmin/core"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var UserSettingGroup = core.Group("/setting/user", newUserHandler, regUser, middleware.TokenAuth())

func newUserHandler(srv setting.IUserService) *userHandler {
	return &userHandler{srv: srv}
}

func regUser(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *userHandler) {
		rg.GET("/detail", handle.detail)
		rg.POST("/save", handle.edit)
	})
}

type userHandler struct {
	srv setting.IUserService
}

// detail user详情
func (uh userHandler) detail(c *gin.Context) {
	res, err := uh.srv.Get(c)
	response.CheckAndRespWithData(c, res, err)
}

// detail user详情
func (uh userHandler) edit(c *gin.Context) {
	var userSetReq req.UserSetReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &userSetReq)) {
		return
	}
	response.CheckAndResp(c, uh.srv.Set(c, userSetReq))
}
